package com.czkt.shop.controller;


import com.alibaba.fastjson.JSON;
import com.czkt.shop.pojo.User;
import com.czkt.shop.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.UUID;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    IUserService userService;

    @Autowired
    StringRedisTemplate redisTemplate;

    @RequestMapping("/login")
    public Object doLogin(String userName,String userPassword, HttpSession session){

        System.out.println("userName" + userName + "/userPassword:" + userPassword);

        String token = UUID.randomUUID().toString();

        User user = new User();
        user.setUserName(userName);
        user.setUserPassword(userPassword);

        User users = userService.getLogin(user);

        if (users != null){

            session.setAttribute("uid",users.getUserId());

            redisTemplate.opsForValue().set(token, JSON.toJSONString(users.getUserId()));

            System.out.println("进去了redis");

            return token;

        } else {

            return 0;

        }

    }

}